/*************************************************************************
	> File Name: P10902.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-04-15 13:54:10
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

long long solve(vector<int> &arr) {
    vector<long long> val;
    int i = 0, j = arr.size() - 1;
    while (i < j) {
        val.push_back(arr[i] - arr[j]);
        i++, j--;
    }
    long long ans = 0;
    for (int i = 0; i < val.size(); i++) {
        ans += abs(val[i]);
        if (i + 1 < val.size() && val[i] > 0 && val[i + 1] > 0) {
            val[i + 1] -= min(val[i], val[i + 1]);
        } else if (i + 1 < val.size() && val[i] < 0 && val[i + 1] < 0) {
            val[i + 1] -= max(val[i], val[i + 1]);
        }
    }
    return ans;
}

int main() {
    int n;
    cin >> n;
    vector<int> arr(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    cout << solve(arr) << endl;
    return 0;
}
